#include "headfiles.h"

using namespace std;

int main(int argc, char **argv) {
    if (argc != 2) {
        cout << "Usage: " << argv[0] << " file" << endl;
        return 0;
    }

    fstream fs;
    fs.open(argv[1], iostream::in);
    if (!fs.is_open()) {
        cout << "File " << argv[1] << " open error!" << endl;
        return 0;
    }

    string str;
    vector<Interval> input;
    while (!fs.eof()) {
        getline(fs, str);
        int loc = str.find(',');
        if (str.empty()) {
            break;
        }

        if (loc == string::npos) {
            cout << "Input error!" << endl;
            return 1;
        }

        Interval val;
        int start = atoi(str.substr(0, loc).c_str());
        int end = atoi(str.substr(loc + 1).c_str());
        val.start = start;
        val.end = end;
        input.push_back(val);
    }
    fs.close();

    for (auto it = input.begin(); it != input.end(); it++) {
        cout << "[" << it->start << ", " << it->end << "]\t";
    }
    cout << endl;

    vector<Interval> output = mergeIntervals(input);

    cout << "Merged intervals are:" << endl;
    for (auto it = output.begin(); it != output.end(); it++) {
        cout << "[" << it->start << ", " << it->end << "]\t";
    }
    cout << endl;

    return 0;
}













